package icegps.com.bhmap.drawhelp;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import icegps.com.bhmap.bean.MultipleEvaluatorHelp;
import icegps.com.bhmap.bean.TargetPoint;
import icegps.com.bhmap.listener.OnUiChangeListener;
import icegps.com.bhmap.view.IMapView;
import timber.log.Timber;

/**
 * Created by 111 on 2017/12/9.
 */

public class MapController {

    private final AnimaHelp animaHelp;
    private IMapView iMapView;
    private float px, py;
    private LinkedBlockingQueue<TargetPoint> targetPointQueue;
    private CyclicBarrier cyclicBarrier;
    private TargetPoint targetPoint;

    public MapController(IMapView iMapView, CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
        targetPointQueue = new LinkedBlockingQueue<>(1000);
        this.iMapView = iMapView;
        animaHelp = new AnimaHelp(iMapView);
        animaHelp.setOnUiChangeListener(onUiChangeListener);
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        targetPoint = targetPointQueue.take();
                        handler.sendEmptyMessage(0);
                        MapController.this.cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            animaHelp.startLocationChange(MapController.this.iMapView.getTargetPoint(), targetPoint);
        }
    };

    /**
     * @param focusX          缩放时候的中心点
     * @param focusY          缩放时候的中心点
     * @param currentMultiple 缩放前的倍数
     * @param scale           需要缩放的倍数
     * @param isAnima         是否走Animahelp
     */
    public void setScale(float focusX, float focusY, float currentMultiple, float scale, boolean isAnima) {
        //缩放时需要的偏移量=(downXY-locationXY)*scale-(downXY-locationXY)
        if (isAnima) {

            px = focusX - iMapView.getTranslateX();
            py = focusY - iMapView.getTranslateY();
            px = px * scale - px;
            py = py * scale - py;

            //设置起始倍数与偏移量
            MultipleEvaluatorHelp startHelp = new MultipleEvaluatorHelp();
            startHelp.setMultiple(iMapView.getMultiple());
            startHelp.setPx(0);
            startHelp.setPy(0);

            //设置目标倍数与偏移量
            MultipleEvaluatorHelp endHelp = new MultipleEvaluatorHelp();
            endHelp.setMultiple(currentMultiple * scale);
            endHelp.setPx(px);
            endHelp.setPy(py);

            //开始走动画(把间距平均,一次回调)
            animaHelp.startMultipleChange(startHelp, endHelp);

        } else {

            MultipleEvaluatorHelp multipleEvaluatorHelp = new MultipleEvaluatorHelp();
            currentMultiple = iMapView.getScaleEndMultiple() * scale;
            Timber.e(iMapView.getScaleEndMultiple() + "");
            px = focusX - (iMapView.getTranslateX());

            py = focusY - (iMapView.getTranslateY());

            px = px * (scale) - px;

            py = py * (scale) - py;

            multipleEvaluatorHelp.setMultiple(currentMultiple);

            multipleEvaluatorHelp.setPx(px);

            multipleEvaluatorHelp.setPy(py);

            iMapView.scaleMap(multipleEvaluatorHelp, AnimaHelp.NO_AINIMA);
        }
    }


    public void setTargetPoint(TargetPoint targetPoint) {
        try {
            targetPointQueue.put(targetPoint);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    OnUiChangeListener onUiChangeListener = new OnUiChangeListener() {
        @Override
        public void onLocationChange(final TargetPoint targetPoint, final float fraction) {
            iMapView.moveLocation(targetPoint, fraction);
        }

        @Override
        public void onMultipleChange(final MultipleEvaluatorHelp multipleEvaluatorHelp, float fraction) {
            iMapView.scaleMap(multipleEvaluatorHelp, fraction);
        }
    };
}
